package br.com.lumis.saas.service.consumerclient.dataprovider;

import java.util.Calendar;
import java.util.List;

import lumis.content.table.ContentTableDataProvider;
import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularData;
import lumis.doui.table.TableSource;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.hyperlink.HyperLinkConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.serviceinterface.IServiceInterfaceUrl;
import lumis.util.ITransaction;
import br.com.lumis.saas.SaasRequestParameters;
import br.com.lumis.saas.service.consumerclient.model.Environment;
import br.com.lumis.saas.service.consumerclient.persistence.dao.ConsumerClientDAO;
import br.com.lumis.saas.util.Configuration;


public class ReportEnvironmentConsolidedDataProvider extends ContentTableDataProvider {
	
	private  ConsumerClientDAO consumerClientDAO =  new ConsumerClientDAO(); 
	
	public ReportEnvironmentConsolidedDataProvider() throws PortalException {
		super();			
	}
		
	public void loadData(SessionConfig sessionConfig, TableSource source,
		ITransaction transaction) throws PortalException {		
		
		Configuration config =  Configuration.getInstance();
		
		List<Environment> environments =  consumerClientDAO.getReportEnvironmentConsolided(sessionConfig,  (lumis.portal.transaction.ITransaction)transaction);
		
		TabularData tabularData = source.getData();
		
		Calendar cal = Calendar.getInstance();		
		int diaPrimeiro = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		int diaUltimo =  cal.get(Calendar.DAY_OF_MONTH);
		int month = cal.get(Calendar.MONTH)+1;
		int year = cal.get(Calendar.YEAR);
		
		String datestart = String.valueOf(diaPrimeiro) +'/' + String.valueOf(month)+'/'+String.valueOf(year);
		String dateend = String.valueOf(diaUltimo) +'/' + String.valueOf(month)+'/'+String.valueOf(year);
							
		for (Environment environment : environments) {							
			ISourceData sourceData = tabularData.addRow();
			sourceData.put(SaasRequestParameters.PAGEVIEW, environment.getConsumerClients().get(0).getPageviews());
			sourceData.put(SaasRequestParameters.TRANSF, environment.getConsumerClients().get(0).getTransfer());
			sourceData.put(SaasRequestParameters.STORAGE, environment.getConsumerClients().get(0).getStorage());
			sourceData.put(SaasRequestParameters.ENVIRONMENTID, environment.getId());							
			sourceData.put(SaasRequestParameters.PAGE_PARAMETER_NAME, environment.getName());
			sourceData.put(SaasRequestParameters.URLSITE, environment.getUrl());
			sourceData.put(SaasRequestParameters.STATUS, environment.getStatus());
			
			sourceData.put(SaasRequestParameters.DATAINICIO, datestart);
			sourceData.put(SaasRequestParameters.DATAFIM, dateend);
						
			double unitsPages = environment.getConsumerClients().get(0).getPageviews() / Double.valueOf(config.getProperty("saas.unit.pageview"));
			double unitsTransfer = environment.getConsumerClients().get(0).getTransfer() / Double.valueOf(config.getProperty("saas.unit.transf"));
			double unitsStorage= environment.getConsumerClients().get(0).getStorage() / Double.valueOf(config.getProperty("saas.unit.storage"));
			
			double mayor = unitsPages;
			
			if(unitsTransfer > mayor )
				mayor =unitsTransfer;
			if(unitsStorage > mayor )
				mayor =unitsStorage;
						
			sourceData.put(SaasRequestParameters.LUMISUNITS, Math.ceil(mayor));
			
			IServiceInterfaceUrl url = getServiceInterfaceHyperLink("saas.service.customer.reportEnvironmentConsolidedDetails", source, sessionConfig, transaction);
			
			if(url != null){
				url.setParameter("environmentId", environment.getId());
				url.setParameter("month", String.valueOf(Calendar.getInstance().get(Calendar.MONTH)+1));
				url.setParameter("year", String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
						
				sourceData.put(SaasRequestParameters.URL, url.toString());
			}
		}											
	}
	
	
	
	protected IServiceInterfaceUrl getServiceInterfaceHyperLink(String destInterfaceId,TableSource source, SessionConfig sessionConfig, ITransaction transaction) throws PortalException
	{
		
		HyperLinkConfig hyperLinkConfig = new HyperLinkConfig( source.getDouiContext().getRequest(), source.getDouiContext().getResponse());
		String serviceId = source.getDouiContext().getRequest().getServiceConfig().getId();

		if (destInterfaceId.indexOf(".") == -1)
			destInterfaceId = serviceId + "." + destInterfaceId;

		// if destination interface is the current service, use current service instance
		if (serviceId.equals(destInterfaceId.substring(0, destInterfaceId.lastIndexOf(".")))
				&& source.getDouiContext().getRequest().getServiceInstanceConfig() != null)
			hyperLinkConfig.setDestServiceInstanceId(source.getDouiContext().getRequest().getServiceInstanceConfig().getId());

		hyperLinkConfig.setDestServiceInterfaceId(destInterfaceId);
		IServiceInterfaceUrl url = ManagerFactory.getHyperLinkManager().getUrl(sessionConfig, hyperLinkConfig, transaction);

		return url;
	}
}
